package com.gitee.zzh.sqllineage.util;

import java.util.ArrayList;
import java.util.List;

public class MyStringUtils {
    public static boolean isBlank(String str) {
        return str == null || "".equals(str.trim());
    }

    public static boolean matchBracket(String text, String leftBracket, String rightBracket) {
        int count1 = 0;
        int count2 = 0;
        for (char ch : text.toCharArray()) {
            if(leftBracket.equals(String.valueOf(ch))) {
                count1++;
            }
            if(rightBracket.equals(String.valueOf(ch))) {
                count2++;
            }
            if(count1 < count2) {
                return false;
            }
        }
        return count1 == count2;
    }

    public static boolean notMatchBracket(String[] parts, String leftBracket, String rightBracket) {
        for (String part : parts) {
            if(!matchBracket(part, leftBracket, rightBracket)) {
                return true;
            }
        }
        return false;
    }

    public static List<String> subStr(String text, String c) {
        List<String> list = new ArrayList<>();
        while (text.contains(c) && text.indexOf(c) < text.lastIndexOf(c)) {
            int index = text.indexOf(c);
            String right = text.substring(index);
            int indexl = right.substring(c.length()).indexOf(c);
            String sonString = right.substring(0, indexl + c.length()) + c;
            list.add(sonString);
            text = right.substring(indexl + 2 * c.length());
        }
        return list;
    }

    public static List<String> bracketSubStr(String text, String subString, String begin, String end) {
        List<String> list = new ArrayList<>();
        while (text.contains(subString)) {
            int index = text.indexOf(subString);
            String right = text.substring(index);
            int leftBracketCount = 0;
            int rightBracketCount = 0;
            int charCount = 0;
            for (char ch : right.toCharArray()) {
                charCount++;
                if (begin.equals(String.valueOf(ch))) {
                    leftBracketCount++;
                }
                if (end.equals(String.valueOf(ch))) {
                    rightBracketCount++;
                }
                if (leftBracketCount > 0 && leftBracketCount == rightBracketCount) {
                    break;
                }
            }
            if (leftBracketCount != rightBracketCount) {
                return list;
            }
            String sonSql = right.substring(0, charCount);
            list.add(sonSql);
            int indexSon = text.indexOf(sonSql);
            text = text.substring(0, indexSon) + text.substring(indexSon + sonSql.length());
        }
        return list;
    }

    public static List<String> getEndBracketSubStr(String text, String subString, String begin, String end) {
        int index = text.indexOf(subString);
        if (index == -1) {
            return new ArrayList<>();
        }
        String textReverse = new StringBuilder(text).reverse().toString();
        String subStringReverse = new StringBuilder(subString).reverse().toString();
        List<String> list = bracketSubStr(textReverse, subStringReverse, end, begin);
        List<String> list1 = new ArrayList<>();
        for (String s : list) {
            list1.add(new StringBuilder(s).reverse().toString());
        }
        return list1;
    }

    public static int getCharCount(String text, String chs) {
        int count = 0;
        for (char ch : text.toCharArray()) {
            if(chs.equals(String.valueOf(ch))) {
                count++;
            }
        }
        return count;
    }

    public static String replaceSpace(String text, String... sArr) {
        return replaceChar(text, " ", sArr);
    }

    public static String replaceChar(String text, String seperator, String... sArr) {
        if(sArr.length > 0) {
            for (String s : sArr) {
                text = text.replace(s, seperator);
            }
        }
        while (text.contains("  ")) {
            text = text.replace("  ", " ");
        }
        return text;
    }

    public static String substringBySymbol(String text, String... symbolArr) {
        if(symbolArr.length == 0) {
            return text;
        }
        for (String s : symbolArr) {
            if(text.contains(s)) {
                text = text.substring(0, text.indexOf(s));
            }
        }
        return text;
    }

    public static String substringByLastSymbol(String text, String... symbolArr) {
        if(symbolArr.length == 0) {
            return text;
        }
        for (String s : symbolArr) {
            if(text.contains(s)) {
                text = text.substring(text.lastIndexOf(s) + s.length());
            }
        }
        return text;
    }

    public static String trimSpace(String text, String... sArr) {
        if(sArr.length == 0) {
            return text;
        }
        for (String s : sArr) {
            if(text.contains(s)) {
                text = text.replace(s + " ", s)
                           .replace(" " + s, s);
            }
        }
        return text;
    }

    public static String addBracketSpace(String text, String... sArr) {
        if(sArr.length == 0) {
            return text;
        }
        for (String s : sArr) {
            if(text.contains(s)) {
                text = text.replace(")" + s + " ", ") " + s + " ")
                           .replace(" " + s + "(", " " + s + " (")
                           .replace(")" + s + "(", ") " + s + " (");
            }
        }
        return text;
    }

    /*========================================used method：转换文本->依赖关系提取====================================*/

    public static boolean contains(String text, String word, String... separators) {
        if (!text.contains(word)) {
            return false;
        }
        if (text.equals(word)) {
            return true;
        }
        for (String separator1 : separators) {
            if (text.contains(separator1)) {
                for (String separator2 : separators) {
                    if (text.contains(separator1 + word + separator2)) {
                        return true;
                    }
                }
            }
        }
        for (String separator : separators) {
            if (text.startsWith(word + separator) || text.endsWith(separator + word)) {
                return true;
            }
        }
        return false;
    }

    public static List<String> getWordAlias(String text, String field) {
        List<String> aliasList = new ArrayList<>();
        while (text.contains("." + field)) {
            String text1 = text.substring(0, text.indexOf("." + field));
            aliasList.add(getSuffixWord(text1));
            text = text.substring(text.indexOf("." + field) + ("." + field).length());
        }
        return aliasList;
    }

    public static String getSuffixWord(String text) {
        int index = 0;
        int subIndex = 0;
        for (char ch : text.toCharArray()) {
            if (!(ch >= 'a' && ch <= 'z') && !(ch >= 'A' && ch <= 'Z') && !(ch >= '0' && ch <= '9')) {
                subIndex = index;
            }
            index++;
        }
        if (subIndex == 0) {
            return text;
        }
        return text.substring(subIndex + 1);
    }

}
